package cn.fresh.xuanyuan.module.member.dal.mysql.message;

import cn.fresh.xuanyuan.framework.common.pojo.PageParam;
import cn.fresh.xuanyuan.framework.common.pojo.PageResult;
import cn.fresh.xuanyuan.framework.mybatis.core.mapper.BaseMapperX;
import cn.fresh.xuanyuan.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.fresh.xuanyuan.module.member.dal.dataobject.message.MessageDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface MessageMapper extends BaseMapperX<MessageDO> {

    default Long getUnread(Long userId) {
        LambdaQueryWrapperX<MessageDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eq(MessageDO::getUserId, userId).eq(MessageDO::getReadStatus, false);
        Long total = selectCount(queryWrapperX);
        return total;
    }

    default Long getUnreadByType(Long userId, Integer msgType) {
        LambdaQueryWrapperX<MessageDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eq(MessageDO::getUserId, userId).eq(MessageDO::getReadStatus, false).eq(MessageDO::getMsgType, msgType);
        Long total = selectCount(queryWrapperX);
        return total;
    }

    default PageResult<MessageDO> getMessageList(Long userId, Integer msgType, PageParam pageParam) {
        LambdaQueryWrapperX<MessageDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eq(MessageDO::getUserId, userId);
        if(msgType != null) {
            queryWrapperX.eq(MessageDO::getMsgType, msgType);
        }
        queryWrapperX.orderByDesc(MessageDO::getCreateTime);
        PageResult<MessageDO> page = selectPage(pageParam, queryWrapperX);
        return page;
    }
}
